<template>
    <div class="widthAll heightAll">
        <el-form :model="bookInfo" :rules="rules" label-position="right"
                 label-width="auto" ref="bookInfo"
                 style="margin: 10px; width: 800px;">
            <el-form-item label="编号">
                <el-input disabled v-model="bookInfo.id"></el-input>
            </el-form-item>
            <el-form-item label="书名" prop="name">
                <el-input v-model="bookInfo.name">
                    <template slot="append"><i class="el-icon-notebook-1"></i></template>
                </el-input>
            </el-form-item>
            <el-form-item label="作者" prop="author">
                <el-input v-model="bookInfo.author">
                    <template slot="append"><i class="el-icon-edit-outline"></i></template>
                </el-input>
            </el-form-item>
            <el-form-item label="分类" prop="category">
                <el-select clearable placeholder="请选择书籍分类" style="margin-right: 15px;"
                           v-model="bookInfo.category">
                    <el-option
                            :key="category.id"
                            :label="category.name"
                            :value="category.id"
                            v-for="category in getCategories">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="库存" prop="count">
                <!---->
                <el-input-number :min="0" label="库存" v-model="bookInfo.count"></el-input-number>
            </el-form-item>
            <el-form-item>
                <el-button @click="submitForm('bookInfo')" type="primary">更新图书</el-button>
                <el-button @click="resetForm()">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>


<script>
import bookInfo from "../../store/bookInfo";
import categoryInfo from "../../store/categoryInfo";
import common from "../../store/common";

export default {
    data() {
        return {
            preBookInfo: null,
            bookInfo: JSON.parse(JSON.stringify(bookInfo.getters.getBookForm)), //本来是写null的，结果报 id of null 错误
            rules: bookInfo.getters.getFormRules,
        };
    },
    methods: {
        submitForm(formName) {
            const _this = this;
            _this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (!_this.isSameInfo(_this)) {
                        bookInfo.dispatch('checkBookExist', _this.bookInfo).then((rs) => {
                            if (rs)
                                _this.showExistBookWarn();
                            else
                                _this.updateBook(_this);
                        })
                    } else {
                        _this.updateBook(_this);
                    }
                }
            });
        },
        updateBook(_this) {
            bookInfo.dispatch('updateBook', _this.bookInfo).then(res => { //没加_this.,重名了
                common.afterUpdateActions(_this, res, '修改', bookInfo.state.routeUrl.bookList, true);
            });
        },
        resetForm() {
            //赋原值
            this.bookInfo = JSON.parse(JSON.stringify(this.preBookInfo));
        }, queryBook(_this) {
            bookInfo.dispatch('queryBookById', _this.$route.query.id).then(resp => {
                /*之前写成 bookinfo = resp;  preBookInfo = resp; 引用相同*/
                _this.bookInfo = JSON.parse(JSON.stringify(resp)); //深拷贝
                _this.preBookInfo = JSON.parse(JSON.stringify(resp)); //因为你一个大小Book->book写就错了
            });
        }, showExistBookWarn() {
            this.$message({type: 'warning', message: '该书名已存在'});
        }, isSameInfo(_this) { //是否更改过原始信息
            return _this.bookInfo.name === _this.preBookInfo.name && _this.bookInfo.author === _this.preBookInfo.author;
        }
    },
    mounted() {
        const _this = this;
        _this.queryBook(_this);
        categoryInfo.dispatch('queryCategoriesAll');
    }, computed: {
        getCategories: function () {
            return categoryInfo.getters.getCategories;
        }
    }
}
</script>